package com.yohann.algorithm.kruskal;

import java.util.Arrays;

/**
 * <p>
 * 克鲁斯卡尔算法(公交站问题)
 * </p>
 *
 * @author Yohann
 * @since 2021/1/24 20:13
 */
public class KruskalAlgorithm {
    public static void main(String[] args) {
        char[] vertexs = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        int[][] matrix = new int[][]{
                {0, 12, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 16, 14},
                {12, 0, 10, Integer.MAX_VALUE, Integer.MAX_VALUE, 7, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, 10, 0, 3, 5, 6, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, 3, 0, 4, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, 5, 4, 0, 2, 8},
                {16, 7, 6, Integer.MAX_VALUE, 2, 0, 9},
                {14, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 8, 9, 0}
        };

        Kruskal kruskal = new Kruskal(vertexs, matrix);
        kruskal.kruskal();
    }
}

class Kruskal {
    //边
    private int edgeNum;
    //顶点
    private char[] vertexs;
    //邻接矩阵
    private int[][] matrix;

    public Kruskal(char[] vertexs, int[][] matrix) {
        this.vertexs = vertexs;
        this.matrix = matrix;

        for (int i = 0; i < vertexs.length; i++) {
            for (int j = i + 1; j < vertexs.length; j++) {
                if (matrix[i][j] != Integer.MAX_VALUE) {
                    edgeNum++;
                }
            }
        }
    }

    public void print() {
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = 0; j < vertexs.length; j++) {
                System.out.printf("%12d", matrix[i][j]);
            }
            System.out.println();
        }
    }

    public void sortEdge(EdgeData[] edges) {
        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - 1 - i; j++) {
                if (edges[j].weight > edges[j + 1].weight) {
                    EdgeData temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }

    public int getPosition(char c) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] == c) {
                return i;
            }
        }

        return -1;
    }

    public EdgeData[] getEdge() {
        int index = 0;
        EdgeData[] edges = new EdgeData[edgeNum];
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = i + 1; j < vertexs.length; j++) {
                if (matrix[i][j] != Integer.MAX_VALUE) {
                    edges[index++] = new EdgeData(vertexs[i], vertexs[j], matrix[i][j]);
                }
            }
        }

        return edges;
    }

    /**
     * 记录顶点的终点
     *
     * @param ends 终点数组
     * @param i    顶点下标
     * @return 终点下表
     */
    public int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }

    public void kruskal() {
        int index = 0;
        //已有生成树的终点
        int[] ends = new int[edgeNum];

        //结果
        EdgeData[] result = new EdgeData[edgeNum];

        //边集合
        EdgeData[] edges = getEdge();
        sortEdge(edges);

        for (EdgeData edge : edges) {
            int start = getPosition(edge.start);
            int end = getPosition(edge.end);

            int n = getEnd(ends, start);
            int m = getEnd(ends, end);
            //终点不一样 不构成回路
            if (n != m) {
                ends[n] = m;
                result[index++] = edge;
            }
        }

        for (int i = 0; i < index; i++) {
            System.out.println(result[i]);
        }
    }
}

class EdgeData {
    char start;
    char end;
    int weight;

    public EdgeData(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "EdgeData{" +
                "start=" + start +
                ", end=" + end +
                ", weight=" + weight +
                '}';
    }
}